<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/6/29 9:05
 * @copyright PanxSoft Inc.
 */

namespace Tests\Utils;


use App\Models\Admin\CustomerAdmin;
use App\Models\Admin\PanxAdmin;
use App\Models\Admin\StoreAdmin;
use App\Models\Customer\Customer;
use App\Models\Customer\DeviceAccount;
use App\Models\Store\Store;
use App\Services\Utils\Guard;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Contracts\JWTSubject;

class FactoryHelper
{
    /**
     * @param array $attributes
     * @return CustomerAdmin
     */
    public static function loginSuperCustomerAdmin($attributes = []): CustomerAdmin
    {
        // faker customer & admin
        $customer = factory(Customer::class)->create();
        /** @var CustomerAdmin $cust_admin */
        $cust_admin = factory(CustomerAdmin::class)->create($attributes +[
            'customer_id' => $customer->id
        ]);

        // sign role and login
        $cust_admin->assignRole(CustomerAdmin::SUPER_MAN);
        self::jwtGuardLogin(Guard::ADMIN_CUSTOMER, $cust_admin);

        // append customer relation
        $cust_admin->customer = $customer;

        return $cust_admin;
    }

    /**
     * @param array $attributes
     *
     * @return PanxAdmin
     */
    public static function loginSuperPanxAdmin($attributes = []): PanxAdmin
    {
        /** @var PanxAdmin $panx_admin */
        $panx_admin = factory(PanxAdmin::class)->create($attributes + [
            'panx_id' => 1
        ]);

        // sign role and login
        $panx_admin->assignRole(PanxAdmin::SUPER_MAN);
        self::jwtGuardLogin(Guard::ADMIN_PANX, $panx_admin);

        return $panx_admin;
    }

    /**
     * @param array $attributes
     * @return StoreAdmin
     */
    public static function loginSuperStoreAdmin($attributes = []): StoreAdmin
    {
        /** @var Customer $customer */
        $customer = factory(Customer::class)->create();

        /** @var CustomerAdmin $cust_admin */
        $cust_admin = factory(CustomerAdmin::class)->create($attributes +[
                'customer_id' => $customer->id
            ]);

        /** @var Store $store */
        $store = factory(Store::class)->create([
            'customer_id' => $customer->id
        ]);
//        $store->customer = $customer;

        /** @var StoreAdmin $store_admin */
        $store_admin = factory(StoreAdmin::class)->create($attributes + [
            'store_id' => $store->id
        ]);

        // sign role and login
        $store_admin->assignRole(StoreAdmin::SUPER_MAN);
        self::jwtGuardLogin(Guard::ADMIN_STORE, $store_admin);

        // append admin relation
        $store_admin->store = $store;
        $store_admin->customer = $customer;
        $store_admin->cust_admin = $cust_admin;

        return $store_admin;
    }
    
    /**
     * @param array $attributes
     * @return DeviceAccount
     */
    public static function loginDeviceAccount($attributes = []): DeviceAccount
    {
        // faker customer & admin
        /** @var Customer $customer */
        $customer = factory(Customer::class)->create();

        /** @var Store $store */
        $store = factory(Store::class)->create([
            'customer_id' => $customer->id
        ]);

        /** @var DeviceAccount $dev_acnt */
        $dev_acnt = factory(DeviceAccount::class)->create($attributes + [
            'customer_id' => $customer->id,
            'store_id'    => $store->id
        ]);

        self::jwtGuardLogin(Guard::APP, $dev_acnt);

        // append device relation
        $dev_acnt->setRelation('customer', $customer);
        $dev_acnt->setRelation('store', $store);

        return $dev_acnt;
    }

    /**
     * @param string $guard
     * @param JWTSubject $admin
     *
     * @return string - jwt token
     */
    public static function jwtGuardLogin(string $guard, JWTSubject $admin): string
    {
        /**
         * @see JWTGuard::login()
         */
        return Auth::guard($guard)->login($admin);
    }

    /**
     * @param string $guard
     * @param JWTSubject $admin
     *
     * @return array
     */
    public static function jwtGuardHeader(string $guard, JWTSubject $admin): array
    {
        $token = self::jwtGuardLogin($guard, $admin);

        return ['Authorization' => "Bearer $token"];
    }
}